package com.gadgets.framework.sequeuce.service;

import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.pojo.Sequeuce;
import com.gadgets.framework.common.repository.ISequeue;
import com.gadgets.framework.common.util.PaginationUtil;
import com.gadgets.framework.sequeuce.dto.SequeuceQueryDTO;
import com.gadgets.framework.sequeuce.dto.SequeuceResetDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author UncleXiu
 * @version v1.0.0
 * @data 2021-06-03
 * @moduel gadgets
 * 
 */
@Service
@Transactional(transactionManager = "gadgetsDbTransactionManage")
public class SequeuceManageServiceImpl implements ISequeuceManageService{

    @Autowired
    private ISequeue sequeue;

    /**
     * 分页查询序列信息
     *
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     * @return 查询结果
     * @throws GadgetsServiceException 异常信息
     */
    @Override
    @Transactional(transactionManager = "gadgetsDbTransactionManage", propagation = Propagation.SUPPORTS)
    public List<Sequeuce> querySequeuceInfo(SequeuceQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException {
        PaginationUtil.bulidPaginationInfo(pagination);
        List<Sequeuce> list = sequeue.selectSequeuceInfo(queryDTO);
        PaginationUtil.getPaginationResultInfo(list,pagination);
        return list;
    }

    /**
     * 重置序列名称
     *
     * @param resetDTO 重置信息
     * @throws GadgetsServiceException 异常信息
     */
    @Override
    @Transactional(transactionManager = "gadgetsDbTransactionManage", propagation = Propagation.REQUIRED,rollbackFor = Throwable.class)
    public void resetSequeuce(SequeuceResetDTO resetDTO) throws GadgetsServiceException {
        int i = sequeue.resetSequeuce(resetDTO);
        if(i != 1){
            throw new GadgetsServiceException("更新序列数不为1，序列可能不存在或存在重名序列，请刷新后再试");
        }
    }
}
